package com.itc.bi.easyExcelListener;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.itc.bi.easyExcelListener
 * @Author: hecaiy
 * @CreateTime: 2024-09-17  18:04
 * @Description: TODO
 * @Version: 1.0
 */
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelAnalysisException;

import java.util.LinkedHashMap;
import java.util.List;

public class ExcelReader {

    // 方法：读取指定 sheet 的前 N 条数据
    public static  List<LinkedHashMap<Integer, String>> readLimitedRowsFromSheet(String filePath, int sheetNo, int limit) {
        // 创建自定义监听器，设置读取条数限制
        LimitedDataListenerss listener = new LimitedDataListenerss(limit);
        //读取文件数据
        try {
            EasyExcel.read(filePath, listener).sheet().doRead();
        } catch (ExcelAnalysisException a) {
            System.out.println("读取完成");
            listener.getDataList();
        } catch (Exception a) {
            System.out.println("读取失败");
            throw a;
        }

        // 读取 Excel 文件中的指定 sheet 和条数
        EasyExcel.read(filePath, listener)
                .sheet(sheetNo)  // 指定 sheet 号，sheetNo 从 0 开始
                .doRead();



        // 返回读取到的数据
        return listener.getDataList();
    }
}

